<template>
  <div class="container">
    <el-form
      ref="registerForm"
      :model="registerForm"
      :rules="registerRules"
      class="registerform"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">用户注册</h3>
      </div>

      <el-form-item prop="name">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="name"
          v-model="registerForm.name"
          :placeholder="'输入用户'"
          name="name"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-form-item prop="fullName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="fullName"
          v-model="registerForm.fullName"
          :placeholder="'输入用户'"
          name="fullName"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="Caps lock is On"
        placement="right"
        manual
      >
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            ref="password"
            v-model="registerForm.password"
            type="password"
            :placeholder="'输入密码'"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleRegister"
          />
        </el-form-item>
      </el-tooltip>

      <el-form-item prop="title">
        <span class="svg-container">
          <svg-icon icon-class="excel" />
        </span>
        <el-input
          ref="title"
          v-model="registerForm.title"
          :placeholder="'输入职务'"
          name="title"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-form-item prop="eMail">
        <span class="svg-container">
          <svg-icon icon-class="email" />
        </span>
        <el-input
          ref="eMail"
          v-model="registerForm.eMail"
          :placeholder="'输入邮箱'"
          name="eMail"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-form-item prop="telephoneNo">
        <span class="svg-container">
          <svg-icon icon-class="phone" />
        </span>
        <el-input
          ref="telephoneNo"
          v-model="registerForm.telephoneNo"
          :placeholder="'输入电话号码'"
          name="telephoneNo"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleRegister"
      >
        注册
      </el-button>

      <div style="position: relative">
        <div class="tips">
          <span>忘记密码？</span>
        </div>
        <div class="regist">
          <span><router-link to="/login">去登录</router-link></span>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Register",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入正确的用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位数"));
      } else {
        callback();
      }
    };
    const validateTitle = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error("职位不能为空"));
      } else {
        callback();
      }
    };
    const validateEmail = (rule, value, callback) => {
      const reg = /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/;
      if (!reg.test(value)) {
        callback(new Error("邮箱格式错误"));
      } else {
        callback();
      }
    };
    const validateTelephone = (rule, value, callback) => {
      const reg1 = /^1\d{10}$/;
      const reg2 = /^0\d{2,3}-?\d{7,8}$/;
      if (!reg1.test(value) && !reg2.test(value)) {
        callback(new Error("电话格式错误"));
      } else {
        callback();
      }
    };
    
    return {
      registerForm: {
        id: 0,
        employeeID: 0,
        name: "",
        statusID: 0,
        fullName: "",
        password: "",
        bUseAD: 0,
        samAccountName: "",
        bChangePwdWhenNextLogin: 0,
        departmentID: 0,
        title: "",
        eMail: "",
        telephoneNo: "",
        employeeID_Update: 0,
        updateTime: "",
      },
      registerRules: {
        name: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        fullName: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        title: [
          { required: true, trigger: "blur", validator: validateTitle },
        ],
        eMail: [
          { required: true, trigger: "blur", validator: validateEmail },
        ],
        telephoneNo: [
          { required: true, trigger: "blur", validator: validateTelephone },
        ],
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.registerForm.name === "") {
      this.$refs.name.focus();
    } else if (this.registerForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.registerForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .registerform {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    // font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    cursor: pointer;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .regist {
    color: white;
    position: absolute;
    right: 0;
    bottom: 6px;
    cursor: pointer;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
